package utils;

import arithmetic2.ListNode;
import arithmetic2.TreeNode;

import java.lang.ref.ReferenceQueue;
import java.util.*;

/**
 * @Author: Jie
 * @Date: 2019/1/21 15:09
 * @Function :打印工具类
 */
public class PrintUtil {

    // 打印函数
    public static void printOneLine(int[] numbers) {
        for (int i = 0; i < numbers.length; i++) {
            System.out.print(numbers[i] + ",");
        }
        System.out.println();
    }

    public static void print(Map map) {
        if (map == null) {
            System.err.println("map == null **************");
            return;
        }
        System.out.println("start print **************");

        Iterator<Map.Entry<Object, Object>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Object, Object> next = iterator.next();
            System.out.println(next.getKey() + "::" + next.getValue());
        }
        System.out.println();
        System.out.println("over print **************");
    }

    public static <T extends Object> void print(List<T> mlist) {
        if (mlist == null) {
            System.err.println("list == null **************");
            return;
        }
        System.out.println("start print **************");
        for (T obj : mlist) {
            System.out.print(obj);
            System.out.print(",");
        }
        System.out.println();
        System.out.println("over print **************");
    }

    public static void printStr(List<String[]> mlist) {
        System.out.println("start print **************");
        for (String[] obj : mlist) {
            for (String str : obj) {
                System.out.println(str);
            }
            System.out.println("------");
        }
        System.out.println("over print **************");
    }

    public static void printArraysChar(char[][] board) {
        System.out.println("start print **************");
        for (char[] a : board) {
            for (int i = 0; i < a.length; i++) {
                if (i == a.length - 1) {
                    System.out.println(a[i]);
                } else {
                    System.out.print(a[i] + " ");
                }
            }
        }
        System.out.println("over print **************");
    }

    public static void printArraysInteger(int[][] board) {
        System.out.println("start print **************");
        for (int[] a : board) {
            for (int i = 0; i < a.length; i++) {
                if (i == a.length - 1) {
                    System.out.println(a[i]);
                } else {
                    System.out.print(a[i] + " ");
                }
            }
        }
        System.out.println("over print **************");
    }

    public static void print(Set set) {
        System.out.println("start print **************");
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        System.out.println("over print **************");
    }

    public static void print(TreeNode treeNode) {
        ArrayList<ArrayList<TreeNode>> listTreeNode = new ArrayList<>();
        if (treeNode == null) {
            System.out.println("treeNode null");
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(treeNode);

        int countColNext = 0;
        while (queue.size() > 0) {
            ArrayList<TreeNode> lineList = new ArrayList<>();
            int size = queue.size();
            while (size > 0) {
                size--;
                TreeNode root = queue.poll();
                TreeNode left = root.left;
                if (left != null) {
                    queue.add(left);
                }
                TreeNode right = root.right;
                if (right != null) {
                    queue.add(right);
                }
                lineList.add(root);
            }

            listTreeNode.add(lineList);
        }

        for (int i = 0; i < listTreeNode.size(); i++) {
            ArrayList<TreeNode> treeNodes = listTreeNode.get(i);
            for (int j = 0; j < treeNodes.size(); j++) {
                System.out.print(treeNodes.get(j).val);
                System.out.print(",");
            }
            System.out.println();
        }

    }


    public static void print(ListNode node) {
        System.out.println("start print **************");
        while (node != null) {
            System.out.println(node.val);
            node = node.next;
        }
        System.out.println("over print **************");
    }

    public static void print(int[] nums) {
        if (nums == null) return;
        System.out.println("start print **************");
        for (int num : nums) {
            System.out.println(num);
        }
        System.out.println("over print **************");
    }

    public static void print(byte[] strTobytes) {
        System.out.println("start print **************");
        for (byte b : strTobytes) {
            System.out.print(b);
        }
        System.out.println();
        System.out.println("over print **************");
    }

    public static void printBit(byte b) {
        System.out.println("start print **************");
        System.out.print(String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'));
        System.out.println("over print **************");
    }
    //左 高，，右面低位。
    // new byte[]{(byte) 0b10101010, (byte) 0b11101100}; // 示例数据 byte[1] 是高位。
    //打印结果： eg: ob11101100, 10101010
    public static void printBytes2Bits(byte[] data) {
        System.out.println("start print **************");
        if (data == null) return;
        StringBuilder binaryString = new StringBuilder();
        binaryString.append("ob");
        for (int i = data.length - 1; i >= 0; i--) {
            for (int j = 7; j >=0 ; j--) {
                // 将每个位右移后与1进行按位与操作，获取该位的值
                binaryString.append((data[i] >> j) & 1);
            }
            if (i > 0) {
                binaryString.append(",");
            }
        }
        System.out.println(binaryString.toString());
        System.out.println("over print **************");
    }

   public static void printHexBytes(byte[] strTobytes) {
        System.out.println("start print **************");
        for (byte b : strTobytes) {
            System.out.printf("0x%02X ", b);
        }
        System.out.println();
        System.out.println("over print **************");
    }

    public static void print(String[] rowList) {
        for (String str : rowList) {
            System.out.print(str);
            System.out.print(",");
        }
    }

    public static void pollAndPrint(ReferenceQueue referenceQueue) {
        if (referenceQueue == null) {
            System.err.println("ReferenceQueue is null");
            return;
        }
        Object obj;
        while (((obj = referenceQueue.poll()) != null)) {
            System.out.println(":" + obj);
        }
        System.out.println();
    }

    public static void print(char[] chars) {
        if (chars == null) return;
        System.out.println("start print **************");
        for (int num : chars) {
            System.out.println(num);
        }
        System.out.println("over print **************");
    }
}
